home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / fract / MiniMand.lha / MiniMand / MiniMand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  9.6 KB  |  397 lines

  1.  
  2. /* Includes here */
  3.  
  4. /// "Includes"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <math.h>
  9. #include <proto/reqtools.h>
  10. #include <proto/iffparse.h>
  11. #include <proto/exec.h>
  12. #include <proto/intuition.h>
  13. #include <proto/graphics.h>
  14. #include <iffp/ilbmapp.h>
  15. #include <exec/exec.h>
  16. #include <exec/execbase.h>
  17. #include <intuition/intuition.h>
  18. #include <libraries/reqtools.h>
  19. ///
  20.  
  21. /* Declarations here */
  22.  
  23. /// "Declarations"
  24. char *Version = "$VER: MiniMand 1.0 (12 Apr 1995)";
  25.  
  26. extern struct ExecBase  *SysBase;
  27. struct IntuitionBase    *IntuitionBase;
  28. struct Library          *IFFParseBase;
  29. struct ReqToolsBase     *ReqToolsBase;
  30.  
  31. struct ILBMInfo ilbm = {0};
  32.  
  33. struct Window           *mywindow = NULL;
  34. struct Screen           *myscreen = NULL;
  35.  
  36. struct TagItem WindowTags[] =
  37. {
  38.         WA_Left,     0,
  39.         WA_Top,      0,
  40.         WA_Width,    NULL,
  41.         WA_Height,   NULL,
  42.         WA_IDCMP,    VANILLAKEY | IDCMP_MOUSEBUTTONS,
  43.         WA_Flags,    WFLG_SMART_REFRESH | WFLG_BORDERLESS |
  44.                      WFLG_BACKDROP | WFLG_ACTIVATE,
  45.         WA_CustomScreen, NULL,
  46.         TAG_DONE
  47. };
  48.  
  49. struct TagItem ScreenTags[] =
  50. {
  51.         SA_Depth, 4,
  52.         SA_Type, CUSTOMSCREEN,
  53.         SA_ShowTitle, FALSE,
  54.         SA_Title, (ULONG)"MiniMand 1.0",
  55.         SA_Colors, NULL,
  56.         SA_Behind, NULL,
  57.         SA_DisplayID, DEFAULT_MONITOR_ID,
  58.         SA_Quiet, TRUE,
  59.         SA_Interleaved, TRUE,
  60.         TAG_DONE
  61. };
  62.  
  63. struct RDArgs *RDArgs;
  64.  
  65. struct
  66. {
  67.         char *xmin;
  68.         char *xmax;
  69.         char *ymin;
  70.         char *ymax;
  71.         long quiet;
  72.         long julia;
  73.         char *jreal;
  74.         char *jimag;
  75.         long hires;
  76.         long lace;
  77.         int *planes;
  78.         int *scrx;
  79.         int *scry;
  80.         int *iter;
  81.         char *save;
  82.         int *palette;
  83.         int *batch;
  84. } Arguments;
  85.  
  86. struct
  87. {
  88.         float xmin;
  89.         float xmax;
  90.         float ymin;
  91.         float ymax;
  92.         int julia;
  93.         float jreal;
  94.         float jimag;
  95.         int scrx;
  96.         int scry;
  97.         int maxiter;
  98.         int cols;
  99. } Vars;
  100.  
  101. struct IntuiMessage *mymessage;
  102. BOOL terminated = FALSE;
  103. clock_t start, end;
  104. int XMouse1, YMouse1, XMouse2, YMouse2;
  105.  
  106. ///
  107.  
  108. /* Functions declared here */
  109.  
  110. void OpenLibs(void);
  111. void Cleanup(int, STRPTR);
  112. void Checkmsg(struct IntuiMessage *mymessage);
  113. void make_mand(void);
  114. void make_julia(void);
  115. void FixArgs(void);
  116.  
  117. /* Main function here */
  118.  
  119. void main(int argc, char *argv[])
  120. /// "Main"
  121. {
  122. int i;
  123.  
  124. Vars.xmin = -2;
  125. Vars.xmax = 2;
  126. Vars.ymin = -2;
  127. Vars.ymax = 2;
  128. Vars.julia = FALSE;
  129. Vars.jreal = -2;
  130. Vars.jimag = 2;
  131. Vars.scrx = 320;
  132. Vars.scry = 256;
  133. Vars.maxiter = 16;
  134. Vars.cols = 16;
  135.  
  136. if(RDArgs = ReadArgs("REMIN/K,REMAX/K,IMIN/K,IMAX/K,QUIET/S,JULIA/S,JREAL/K,JIMAG/K,HIRES/S,LACE/S,PLANES/K/N,SCRX/K/N,SCRY/K/N,ITER/K/N,SAVE/K,PALETTE/S,BATCH/S", (long *) &Arguments, NULL))
  137.     FixArgs();
  138.  
  139. OpenLibs();
  140.  
  141. if(SysBase->AttnFlags & AFF_68881)
  142.  
  143. myscreen = OpenScreenTagList(NULL, ScreenTags);
  144.  
  145. if (!myscreen)
  146.     Cleanup(10, "Unable to open screen");
  147.  
  148. WindowTags[2].ti_Data = (ULONG)Vars.scrx;
  149. WindowTags[3].ti_Data = (ULONG)Vars.scry;
  150. WindowTags[6].ti_Data = (ULONG)myscreen;
  151.  
  152. mywindow = OpenWindowTagList(NULL, WindowTags);
  153.  
  154. if (!mywindow)
  155.     Cleanup(10, "Unable to open window");
  156.  
  157. if(Arguments.palette)
  158.     i=rtPaletteRequest("Choose colors", NULL, RT_Screen, myscreen);
  159. if(!Arguments.palette || i == -1)
  160.     for(i = 0; i < Vars.cols; i++)
  161.         SetRGB32(&myscreen->ViewPort, i, (i*255 / (Vars.cols-1)) << 24, 0, (i*255 / (Vars.cols-1)) << 24);
  162. if (Vars.julia)
  163.         make_julia();
  164. else
  165.         make_mand();
  166. if (!Arguments.batch)
  167. while(!terminated)
  168. {
  169.         Wait( 1 << mywindow->UserPort->mp_SigBit);
  170.         while( mymessage = (struct IntuiMessage *)GetMsg(mywindow->UserPort))
  171.                 Checkmsg(mymessage);
  172. }
  173.  
  174. if(Arguments.save)
  175.     screensave(&ilbm, myscreen, NULL, NULL, Arguments.save);  
  176.  
  177. printf("Approx time used = %d s\n",(end - start)/CLOCKS_PER_SEC);
  178. Cleanup(0, NULL);
  179.  
  180. }
  181. ///
  182.  
  183. /* Functions here */
  184.  
  185. void OpenLibs(void)
  186. /// "OpenLibs"
  187. {
  188. if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",36L)))
  189.     Cleanup(10, "Unable to open intuition.library v36 or higher");
  190. if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L)))
  191.     Cleanup(10, "Unable to open graphics.library");
  192. if (!(ReqToolsBase = (struct ReqToolsBase *)OpenLibrary("reqtools.library",0L)))
  193.     Cleanup(10, "Unable to open reqtools.library");
  194. if (!(IFFParseBase = OpenLibrary("iffparse.library",0L)))
  195.     Cleanup(10, "Unable to open iffparse.library");
  196. if(!(ilbm.ParseInfo.iff = AllocIFF()))
  197.     Cleanup(10, "Unable to allocate IFF");
  198. }
  199. ///
  200.  
  201. void Cleanup(int return_code, STRPTR return_text)
  202. /// "Cleanup"
  203. {
  204. if (return_code)
  205.         printf("%d: %s\n", return_code, return_text);
  206.  
  207. if (ilbm.ParseInfo.iff) FreeIFF(ilbm.ParseInfo.iff);
  208.  
  209. if (mywindow) CloseWindow(mywindow);
  210.  
  211. if (myscreen) CloseScreen(myscreen);
  212.  
  213. if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  214.  
  215. if (GfxBase) CloseLibrary ((struct Library *)GfxBase);
  216.  
  217. if (IFFParseBase) CloseLibrary(IFFParseBase);
  218.  
  219. if (ReqToolsBase) CloseLibrary((struct Library *)ReqToolsBase);
  220.  
  221. exit(NULL);
  222. }
  223. ///
  224.  
  225. void Checkmsg(struct IntuiMessage *mymessage)
  226. /// "Checkmsg"
  227. {
  228. float xdif = Vars.xmax - Vars.xmin, ydif = Vars.ymax - Vars.ymin;
  229.  
  230. ReplyMsg((struct Message *)mymessage);
  231.  
  232. switch(mymessage->Class) {
  233.     case VANILLAKEY:
  234.     if(mymessage->Code == 0x1B)
  235.         terminated=TRUE;
  236.         break;
  237.     case IDCMP_MOUSEBUTTONS:
  238.         if(mymessage->Code == SELECTDOWN) {
  239.             XMouse1 = mymessage->MouseX;
  240.             YMouse1 = mymessage->MouseY;
  241.             // printf("Ned X = %d Y = %d\n",XMouse1,YMouse1);
  242.             }
  243.         if(mymessage->Code == SELECTUP) {
  244.             XMouse2 = mymessage->MouseX;
  245.             YMouse2 = mymessage->MouseY;
  246.             // printf("Upp X = %d Y = %d\n",XMouse2,YMouse2);
  247.             Vars.xmax = xdif * MAX(XMouse1,XMouse2) / Vars.scrx + Vars.xmin;
  248.             Vars.xmin = xdif * MIN(XMouse1,XMouse2) / Vars.scrx + Vars.xmin;
  249.             Vars.ymax = ydif * MAX(YMouse1,YMouse2) / Vars.scry + Vars.ymin;
  250.             Vars.ymin = ydif * MIN(YMouse1,YMouse2) / Vars.scry + Vars.ymin;
  251.             printf("REMAX = %f, REMIN = %f, IMAX = %f, IMIN = %f\n\n",Vars.xmax, Vars.xmin, Vars.ymax, Vars.ymin);
  252.             if(Vars.julia)
  253.                 make_julia();
  254.             else
  255.                 make_mand();
  256.             }
  257.         break;
  258.         }
  259. }
  260. ///
  261.  
  262. void make_mand(void)
  263. /// "make_mand"
  264. {
  265. register int n, sx, sy;
  266. float x, y, t, zr, zi;
  267. float xdif, ydif, ystep, xstep;
  268.  
  269. xdif = Vars.xmax-Vars.xmin;
  270. ydif = Vars.ymax-Vars.ymin;
  271. ystep = (ydif)/Vars.scry;
  272. xstep = (xdif)/Vars.scrx;
  273.  
  274. start = clock();
  275. for(sy = 0, y=Vars.ymin; sy < Vars.scry; sy++)
  276. {               
  277.         for(sx = 0, x=Vars.xmin; sx < Vars.scrx; sx++)
  278.         {
  279.                 zr=x;
  280.                 zi=y;
  281.                 for(n=1; (zr*zr+zi*zi < 4) && (n<Vars.maxiter); n++)
  282.                 {
  283.                         t=zr*zr-zi*zi+x;
  284.                         zi=zr*zi*2+y;
  285.                         zr=t;
  286.                 }
  287.                 SetAPen(mywindow->RPort, n * Vars.cols / Vars.maxiter);
  288.                 WritePixel(mywindow->RPort, sx, sy);
  289.                 if(mymessage = (struct IntuiMessage *)GetMsg(mywindow->UserPort))
  290.                         Checkmsg(mymessage);
  291.                 x+=xstep;
  292.         }
  293.         if(terminated==TRUE)
  294.                 Cleanup(0, NULL);
  295.         y+=ystep;
  296. }
  297. end = clock();
  298. }
  299. ///
  300.  
  301. void make_julia(void)
  302. /// "make_julia"
  303. {
  304. register int n, sx, sy;
  305. float x, y, t, zr, zi;
  306. float xdif, ydif, ystep, xstep;
  307. float ci = Vars.jimag, cr = Vars.jreal;
  308.  
  309. xdif = Vars.xmax-Vars.xmin;
  310. ydif = Vars.ymax-Vars.ymin;
  311. ystep = (ydif)/Vars.scry;
  312. xstep = (xdif)/Vars.scrx;
  313.  
  314. start = clock();
  315. for(sy = 0, y=Vars.ymin; sy < Vars.scry; sy++)
  316. {               
  317.         for(sx = 0, x=Vars.xmin; sx < Vars.scrx; sx++)
  318.         {
  319.                 zr=x;
  320.                 zi=y;
  321.                 for(n=1; (zr*zr+zi*zi < 4) && (n<Vars.maxiter); n++)
  322.                 {
  323.                         t=zr*zr-zi*zi+ci;
  324.                         zi=zr*zi*2+cr;
  325.                         zr=t;
  326.                 }
  327.                 SetAPen(mywindow->RPort, n * Vars.cols / Vars.maxiter);
  328.                 WritePixel(mywindow->RPort, sx, sy);
  329.                 if(mymessage = (struct IntuiMessage *)GetMsg(mywindow->UserPort))
  330.                         Checkmsg(mymessage);
  331.                 x+=xstep;
  332.         }
  333.         if(terminated==TRUE)
  334.                 Cleanup(0, NULL);
  335.         y+=ystep;
  336. }
  337. end = clock();
  338. }
  339. ///
  340.  
  341. void FixArgs(void)
  342. /// "FixArgs"
  343. {
  344. if(Arguments.xmin)
  345.         Vars.xmin=atof(Arguments.xmin);
  346. if(Arguments.xmax)
  347.         Vars.xmax=atof(Arguments.xmax);
  348. if(Arguments.ymin)
  349.         Vars.ymax=-atof(Arguments.ymin);
  350. if(Arguments.ymax)
  351.         Vars.ymin=-atof(Arguments.ymax);
  352. if(Arguments.quiet || Arguments.batch)
  353. {
  354.         ScreenTags[5].ti_Data = TRUE;
  355.         WindowTags[5].ti_Data ^= WFLG_ACTIVATE;
  356. }
  357. if(Arguments.julia)
  358. {
  359.         Vars.julia = TRUE;
  360.         if(Arguments.jreal)
  361.                 Vars.jreal=atof(Arguments.jreal);
  362.         if(Arguments.jimag)
  363.                 Vars.jimag=atof(Arguments.jimag);
  364. }
  365. if(Arguments.hires && Arguments.lace)
  366. {
  367.         ScreenTags[6].ti_Data |= HIRESLACE_KEY;
  368.         Vars.scrx=640;
  369.         Vars.scry=512;
  370. }
  371. else
  372. {
  373.         if(Arguments.hires)
  374.         {
  375.                 ScreenTags[6].ti_Data |= HIRES_KEY;
  376.                 Vars.scrx=640;
  377.         }
  378.         if(Arguments.lace)
  379.         {
  380.                 ScreenTags[6].ti_Data |= LORESLACE_KEY;
  381.                 Vars.scry=512;
  382.         }
  383. }
  384. if(Arguments.planes)
  385. {
  386.     ScreenTags[0].ti_Data = *Arguments.planes;
  387.     Vars.cols = 1 << *Arguments.planes;
  388. }
  389. if(Arguments.scrx)
  390.         Vars.scrx=*Arguments.scrx;
  391. if(Arguments.scry)
  392.         Vars.scry=*Arguments.scry;
  393. if(Arguments.iter)
  394.         Vars.maxiter=*Arguments.iter;
  395. }
  396. ///
  397.